package com.org.qrtrace.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.org.qrtrace.pojo.Phistory;
import com.org.qrtrace.pojo.Product;
import com.org.qrtrace.service.IAccountService;
import com.org.qrtrace.service.ICheckhistoryService;
import com.org.qrtrace.service.IPhistoryService;
import com.org.qrtrace.utils.JsonResult;
import com.org.qrtrace.utils.QRCodeUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/code")
@Api(tags = "二维码生成api")
public class CodeController {
    @Autowired
    private IAccountService accountService;
    @Autowired
    private ICheckhistoryService checkhistoryService;
    @Autowired
    private IPhistoryService phistoryService;

    @GetMapping("/users")
    public JsonResult<?> getUsers(){
        String res = QRCodeUtil.createQRCodeAndReturnBase64(Arrays.toString(accountService.list().toArray()),300,300);
        return JsonResult.successInfo(res);
    }

    @GetMapping("/productHistory")
    public JsonResult<?> getProductHistory(Product product){
        List<Phistory> lists = new ArrayList<>();
        QueryWrapper<Phistory> wrapper = new QueryWrapper<>();
        wrapper.eq("puid",product.getPuid());
        lists = phistoryService.list(wrapper);
        String res = QRCodeUtil.createQRCodeAndReturnBase64(JSON.toJSONString(lists, String.valueOf(true)),300,300);
        return JsonResult.successInfo(res);
    }

    @GetMapping("/product")
    public JsonResult<?> getProduct(Product product){
        String local = "http://8.130.78.116:8888/productDetailsUser/" + product.getPuid();
        String res = QRCodeUtil.createQRCodeAndReturnBase64(local,300,300);
        return JsonResult.successInfo(res);
    }
}
